package com.nmf.timer;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.nmf.dao.gen.BaseCitiesMapper;
import com.nmf.dao.gen.BaseProvincesMapper;
import com.nmf.dao.gen.BizSysAreaMapper;
import com.nmf.dao.gen.bean.BaseCities;
import com.nmf.dao.gen.bean.BaseCitiesExample;
import com.nmf.dao.gen.bean.BaseProvinces;
import com.nmf.dao.gen.bean.BaseProvincesExample;
import com.nmf.dao.gen.bean.BizSysArea;
import com.nmf.dao.gen.bean.BizSysAreaExample;
import com.nmf.site.common.enums.AreaTypeEnums;
import com.nmf.site.common.enums.DelFlagEnums;
import com.nmf.site.common.utils.IdGenerater;

public class HwMain {
	public static void main(String[] args) {
		Logger log = LoggerFactory.getLogger(HwMain.class);
		try {
			String springFileXml = "classpath*:/spring-context*.xml";
			log.info(springFileXml);
			ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(springFileXml);

			BaseProvincesMapper baseProvincesMapper = applicationContext.getBean(BaseProvincesMapper.class);
			
			BaseProvincesExample baseProvincesExample = new BaseProvincesExample();
			List<BaseProvinces>  bplist = baseProvincesMapper.selectByExample(baseProvincesExample);
			
			BaseCitiesMapper baseCitiesMapper = applicationContext.getBean(BaseCitiesMapper.class);
			for(BaseProvinces baseProvinces : bplist) {
				
				insertSysArea(baseProvinces.getProvinceName(),baseProvinces.getId(),0+"",0+"",AreaTypeEnums.PROVINCE,null,applicationContext);
				BaseCitiesExample baseCitiesExample = new BaseCitiesExample();
				BaseCitiesExample.Criteria cityCriteria = baseCitiesExample.createCriteria();
				cityCriteria.andProvinceIdEqualTo(baseProvinces.getId());
				List<BaseCities> bcityList = baseCitiesMapper.selectByExample(baseCitiesExample);
				log.info("插入【"+baseProvinces.getProvinceName()+"】成功");
				for(BaseCities baseCities : bcityList) {
					insertSysArea(baseCities.getCityName(),baseCities.getId(),baseProvinces.getId(),baseProvinces.getId()+",0",AreaTypeEnums.CITY,baseCities.getPoiLocation(),applicationContext);
					log.info("插入【"+baseCities.getCityName()+"】成功");
				}
				
			}
			
			
			log.info("更新完毕");
		} catch (Exception e) {
			log.error("",e);
		}
	}
	
	private static long provinceNum = 100;
	private static long cityNum = 1000;
	private static void insertSysArea(String name,String id ,String parentId,String parentIds,AreaTypeEnums areaTypeEnums,String location,ClassPathXmlApplicationContext applicationContext) {
		BizSysAreaMapper bizSysAreaMapper = applicationContext.getBean(BizSysAreaMapper.class);
		
		BizSysAreaExample bizSysAreaExample = new BizSysAreaExample();
		BizSysAreaExample.Criteria croi = bizSysAreaExample.createCriteria();
		
		croi.andNameEqualTo(name);
		List<BizSysArea> bdlist = bizSysAreaMapper.selectByExample(bizSysAreaExample);
		
		Date date = new Date();
		
		if(bdlist.size()>0) {
			BizSysArea bizSysAreaDb = bdlist.get(0);
			bizSysAreaDb.setCreateDate(date);
			bizSysAreaDb.setDelFlag(DelFlagEnums.DEL_FLAG_NORMAL.value+"");
			bizSysAreaDb.setId(bizSysAreaDb.getId());
			bizSysAreaDb.setName(name);
			bizSysAreaDb.setParentId(parentId);
			bizSysAreaDb.setParentIds(parentIds);
			bizSysAreaDb.setType(areaTypeEnums.value+"");
			bizSysAreaDb.setCreateBy(1+"");
			if(AreaTypeEnums.CITY.equals(areaTypeEnums)) {
				cityNum ++;
				bizSysAreaDb.setSort(cityNum);
			}else if(AreaTypeEnums.PROVINCE.equals(areaTypeEnums)) {
				provinceNum ++ ;
				bizSysAreaDb.setSort(provinceNum);
			}
			
			bizSysAreaDb.setUpdateDate(date);
			bizSysAreaDb.setLocation(location);
			bizSysAreaDb.setUpdateBy(1+"");
			bizSysAreaMapper.updateByPrimaryKeySelective(bizSysAreaDb);
		}else {
			BizSysArea bizSysAreaProvince = new BizSysArea();
			bizSysAreaProvince.setCreateDate(date);
			bizSysAreaProvince.setDelFlag(DelFlagEnums.DEL_FLAG_NORMAL.value+"");
			bizSysAreaProvince.setId(id);
			bizSysAreaProvince.setName(name);
			bizSysAreaProvince.setParentId(parentId);
			bizSysAreaProvince.setParentIds(parentIds);
			bizSysAreaProvince.setType(areaTypeEnums.value+"");
			if(AreaTypeEnums.CITY.equals(areaTypeEnums)) {
				cityNum ++;
				bizSysAreaProvince.setSort(cityNum);
			}else if(AreaTypeEnums.PROVINCE.equals(areaTypeEnums)) {
				provinceNum ++ ;
				bizSysAreaProvince.setSort(provinceNum);
			}
			bizSysAreaProvince.setCreateBy(1+"");
			bizSysAreaProvince.setUpdateDate(date);
			bizSysAreaProvince.setLocation(location);
			bizSysAreaProvince.setUpdateBy(1+"");
			bizSysAreaMapper.insert(bizSysAreaProvince);
		}
	}

}
